package cn.hchaojie.camp.week1;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class CountAndSay {
	private static final String[] DATA = {
			"1",
			"11",
			"21",
			"1211",
			"111221",
			"312211",
			"13112221",
			"1113213211"
	};
	
	@Test
	public void verify() {
		System.out.println(count(1));
		System.out.println(count(2));
		System.out.println(count(3));
		System.out.println(count(4));
		System.out.println(count(5));
		System.out.println(count(6));
		System.out.println(count(7));
//		for (int i = 1; i <= DATA.length; i++) {
//			assertTrue(Integer.valueOf(countAndSay(i)) == DATA[i - 1]);
//		}
	}

	public String countAndSay(int n) {
		return "";
	}

	public String count(int n) {
		if (n == 1) return "1";
		
		// 当前报数需要根据上一个报数来读
		String number = count(n - 1);
		
		StringBuilder sb = new StringBuilder();
		
		int count = 0;
		int index = 0;
		int len = number.length();
		char lastDigit = number.charAt(0);
		while (index < len) {
			char digit = number.charAt(index);

			if (digit == lastDigit) {
				count++;
			} else {
				sb.append(count);		// 插入上个数字的计数
				sb.append(lastDigit);	// 插入上个数字

				count = 1;				// 重置计数
				lastDigit = digit;		// 记录当前数字
			}
			
			index++;
		}
		
		sb.append(count);		// 插入最后一个数字的计数
		sb.append(lastDigit);	// 插入最后数字

		return sb.toString();
	}
}
